#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
增强版MCP服务反馈处理演示
展示优化后的反馈处理策略如何工作
"""

import sys
import os

# 添加项目根目录到路径中
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.services.mcp_service import mcp_service_manager


def demo_enhanced_mcp_feedback():
    """演示增强版MCP服务反馈处理"""
    print("=== 增强版MCP服务反馈处理演示 ===\n")
    
    # 模拟服务器名称
    server_name = "filesystem"
    user_id = "user_123"
    
    print(f"1. 添加用户反馈到 {server_name} 服务")
    
    # 添加不同类型反馈以测试不同策略
    print("\n场景1: 添加少量一般反馈")
    general_feedbacks = [
        {
            "server_name": server_name,
            "user_id": user_id,
            "feedback": "界面可以更直观一些",
            "feedback_type": "general"
        },
        {
            "server_name": server_name,
            "user_id": "user_456",
            "feedback": "操作流程有点复杂",
            "feedback_type": "general"
        }
    ]
    
    # 添加反馈
    for i, feedback_data in enumerate(general_feedbacks, 1):
        result = mcp_service_manager.add_feedback(
            server_name=feedback_data["server_name"],
            user_id=feedback_data["user_id"],
            feedback=feedback_data["feedback"],
            feedback_type=feedback_data["feedback_type"]
        )
        
        if result["success"]:
            print(f"  ✓ 反馈 {i} 添加成功 (ID: {result['feedback_id']})")
        else:
            print(f"  ✗ 反馈 {i} 添加失败: {result['error']}")
    
    print("\n处理少量一般反馈 (自动策略):")
    process_result = mcp_service_manager.process_feedback(server_name, strategy="auto")
    print(f"  结果: {process_result['message']}")
    if "actions_taken" in process_result:
        print(f"  采取的行动: {process_result['actions_taken']}")
    
    print("\n" + "="*50)
    
    print("\n场景2: 添加性能相关反馈")
    performance_feedbacks = [
        {
            "server_name": server_name,
            "user_id": user_id,
            "feedback": "文件读取速度较慢，特别是在处理大文件时",
            "feedback_type": "performance"
        },
        {
            "server_name": server_name,
            "user_id": "user_789",
            "feedback": "响应时间过长",
            "feedback_type": "performance"
        },
        {
            "server_name": server_name,
            "user_id": "user_101",
            "feedback": "批量操作时系统卡顿",
            "feedback_type": "performance"
        }
    ]
    
    # 添加性能反馈
    for i, feedback_data in enumerate(performance_feedbacks, 1):
        result = mcp_service_manager.add_feedback(
            server_name=feedback_data["server_name"],
            user_id=feedback_data["user_id"],
            feedback=feedback_data["feedback"],
            feedback_type=feedback_data["feedback_type"]
        )
        
        if result["success"]:
            print(f"  ✓ 性能反馈 {i} 添加成功 (ID: {result['feedback_id']})")
        else:
            print(f"  ✗ 性能反馈 {i} 添加失败: {result['error']}")
    
    print("\n处理性能反馈 (自动策略):")
    process_result = mcp_service_manager.process_feedback(server_name, strategy="auto")
    print(f"  结果: {process_result['message']}")
    if "actions_taken" in process_result:
        print(f"  采取的行动: {process_result['actions_taken']}")
    if "total_severity" in process_result:
        print(f"  总严重性评分: {process_result['total_severity']}")
    
    print("\n" + "="*50)
    
    print("\n场景3: 添加错误反馈")
    error_feedbacks = [
        {
            "server_name": server_name,
            "user_id": user_id,
            "feedback": "有时会出现连接超时错误",
            "feedback_type": "error"
        },
        {
            "server_name": server_name,
            "user_id": "user_202",
            "feedback": "操作过程中偶尔会崩溃",
            "feedback_type": "error"
        },
        {
            "server_name": server_name,
            "user_id": "user_303",
            "feedback": "文件上传功能有时失败",
            "feedback_type": "error"
        },
        {
            "server_name": server_name,
            "user_id": "user_404",
            "feedback": "权限验证偶尔出错",
            "feedback_type": "error"
        }
    ]
    
    # 添加错误反馈
    for i, feedback_data in enumerate(error_feedbacks, 1):
        result = mcp_service_manager.add_feedback(
            server_name=feedback_data["server_name"],
            user_id=feedback_data["user_id"],
            feedback=feedback_data["feedback"],
            feedback_type=feedback_data["feedback_type"]
        )
        
        if result["success"]:
            print(f"  ✓ 错误反馈 {i} 添加成功 (ID: {result['feedback_id']})")
        else:
            print(f"  ✗ 错误反馈 {i} 添加失败: {result['error']}")
    
    print("\n处理错误反馈 (自动策略):")
    process_result = mcp_service_manager.process_feedback(server_name, strategy="auto")
    print(f"  结果: {process_result['message']}")
    if "actions_taken" in process_result:
        print(f"  采取的行动: {process_result['actions_taken']}")
    if "total_severity" in process_result:
        print(f"  总严重性评分: {process_result['total_severity']}")
    
    print("\n" + "="*50)
    
    print("\n场景4: 查看反馈摘要")
    feedback_summary = mcp_service_manager.get_feedback_summary(server_name)
    print(f"  服务器: {feedback_summary['server_name']}")
    print(f"  总反馈数: {feedback_summary['total_feedback']}")
    print("  按类型统计:")
    for feedback_type, count in feedback_summary['feedback_by_type'].items():
        print(f"    {feedback_type}: {count}")
    
    if feedback_summary['last_feedback']:
        last_fb = feedback_summary['last_feedback']
        print(f"  最新反馈: [{last_fb['feedback_type']}] {last_fb['feedback']} (用户: {last_fb['user_id']})")
    
    print("\n" + "="*50)
    
    print("\n场景5: 使用不同处理策略")
    print("使用重启策略处理反馈:")
    process_result = mcp_service_manager.process_feedback(server_name, strategy="restart")
    print(f"  结果: {process_result['message']}")
    if "actions_taken" in process_result:
        print(f"  采取的行动: {process_result['actions_taken']}")
    
    print("\n使用重新配置策略处理反馈:")
    process_result = mcp_service_manager.process_feedback(server_name, strategy="reconfigure")
    print(f"  结果: {process_result['message']}")
    if "actions_taken" in process_result:
        print(f"  采取的行动: {process_result['actions_taken']}")
    
    print("\n=== 演示完成 ===")


if __name__ == "__main__":
    demo_enhanced_mcp_feedback()